﻿using System;

namespace DB
{
    public class DBFactory
    {
        private static readonly string dbConfig = System.Configuration.ConfigurationManager.AppSettings["dbConfig"];
        private static DBHelper db = null;
        static string assembly;
        static string typeName;
        public static DBHelper GetDBHelper()
        {
            if (db == null)
            {
                if (string.IsNullOrEmpty(dbConfig))
                {
                    return new SQLHelper();
                    //assembly = typeof(DBFactory).Module.Name.Substring(0, typeof(DBFactory).Module.Name.Length - 4);
                    //typeName = typeof(DBFactory).Namespace + ".SQLHelper";
                }
                if (string.IsNullOrEmpty(assembly))
                    assembly = dbConfig.Split(',')[0];
                if (string.IsNullOrEmpty(typeName))
                    typeName = dbConfig.Split(',')[1];
                db = (DBHelper)Activator.CreateInstance(assembly, typeName).Unwrap(); //new SQLHelper();
                return db;
            }
            else
                return db;
        }
    }
}
